<?php

/**
 * 友帮支付回调
 */

class efpaly_notifyControl extends Control {


    public function indexOp(){

        require_once BASE_DATA_PATH.'/vendors/yfpay/yifupay.php';
        $prefix = BASE_DATA_PATH."/vendors/weixin";
        include_once($prefix."/log_.php");
        $json = $GLOBALS['HTTP_RAW_POST_DATA'];
        $data = json_decode($json, true);
        $time = date('Y-m-d', time());
        $log_ = new Log_();
        $log_name = BASE_DATA_PATH."/log/yifu/" . $time . ".log";
        if(!file_exists($log_name)){
            $fp=fopen("$log_name", "w+");
        }
        $log_->log_result($log_name, "【接收到的notify通知】:\n".$json."\n");

        if($data['status']!=='900'){
            $log_->log_result($log_name, "【支付失败】:\n".$data."\n");
        }
        if($data['data']['orderstatus'] !== '0000'){
            $log_->log_result($log_name, "【支付失败】:\n".$data."\n");
        }

       $yifu = new yifupay();

        /*
        $result = array();
        $result['out_trade_no'] = $data['data']['out_trade_no'];
        $result['total_fee'] = $data['data']['total_fee'];
        $result['orderstatus'] = $data['data']['orderstatus'];
        $result['transaction_id'] = $data['data']['transaction_id'];
        $result['secondtimestamp'] = $data['data']['secondtimestamp'];
        $result['nonce_str'] = $data['data']['nonce_str'];
        $result['memo'] = $data['data']['memo'];*/


        ksort($data['data']);
        $sign_arr = array();
        foreach ($data['data'] as $key => $value) {
            if ($key == 'sign' || $value == '') {
                continue;
            }
            $sign_arr[] = $key.'='.$value;
        }
        $sign_arr[] = "key=".$yifu::KEY;
        $sign_str = implode('&', $sign_arr);
        $sign = strtoupper(md5($sign_str));

       // $stringA = $yifu->getSignContent($result);
       /* $stringSignTemp = $stringA.'&key='.'cjkj@e567Ui@0cp8_7irau';
        $log_->log_result($log_name, "【签名比对】:\n".$sign_str."\n".$stringSignTemp."\n");exit;*/
        //$sign = $yifu->getSign($stringA);

        if($sign == $data['data']['sign']){
            //验证成功
            if($data['status'] =='900' && !empty($data['data']['out_trade_no'])){
                $redis = new Redis();
                $deploy = C('redis');
                $redis->connect($deploy['master']['host'],$deploy['master']['port']);
                //支付成功
                $model = Model('bargain_order');
                $bargain_mode = Model('bargain');
                $bg_order_info = $model->getBgOrderInfo(array('bg_order_sn'=>$data['data']['out_trade_no']));
                if(empty($bg_order_info)){
                    //查询是否是朋友代付
                    $bargain_paid = Model('bargain_paid');
                    $bargain_count_model = Model('bargain_count');
                    $bargain_paid_info = $bargain_paid->getBargainpaidInfo(array('paid_sn'=>$data['data']['out_trade_no']));

                    if(!empty($bargain_paid_info)){
                        //改变订单价格
                        $order_info = $model->getBgOrderInfo(array('bg_order_id'=>$bargain_paid_info['bargain_order_id']));
                        $bg_order_price = $order_info['bg_order_price']-$bargain_paid_info['paid_size'];
                        //更改此条活动订单的价格
                        $res = $model->updateBgOrder(array('bg_order_id'=>$order_info['bg_order_id']),array(
                            'bg_order_price'=>$bg_order_price,
                        ));

                        //朋友帮砍表中修改代付总金额
                        $coun_info = $bargain_count_model->getbargain_countInfo(array('member_id'=>$bargain_paid_info['member_id'],'bg_order_id'=>$bargain_paid_info['bargain_order_id']));
                        $paid_sunm = $coun_info['paid_sunm']+$bargain_paid_info['paid_size'];//代付总金额加一;
                        $bargain_count_model->upcountdate(array('member_id'=>$bargain_paid_info['member_id'],'bg_order_id'=>$bargain_paid_info['bargain_order_id']),array('paid_sunm'=>$paid_sunm));

                        //修改朋友代付的记录
                        $ress = $bargain_paid->updateBarginpaid(array('paid_sn'=>$bargain_paid_info['paid_sn']),array('add_time'=>time(),'pay_type'=>'1'));
                        $redis->delete($deploy['prefix'].'payorder-'.$bg_order_info['bg_order_id']);//释放redis中订单支付锁
                        if($res && $ress){
                            exit('success');
                        }

                    }
                }else{

                    $outcome = array();
                    if($bg_order_info['type'] == '1'){
                             //生成兑换码
                        $rand_code = '';
                        for ($i = 0; $i < 16; $i++) {
                            $rand_code .= mt_rand(0, 10);
                        }
                        $code = str_pad($bg_order_info['member_id'].($bg_order_info['bg_order_id']+$bg_order_info['merchant_id']+$bg_order_info['merchant_id']),16,$rand_code);//生成兑换码
                        $outcome['bg_order_code'] = $code;
                        //$bargain_mode->true_member($bg_order_info['bargain_id']);
                    }
                    $outcome['bg_order_status'] = 20;
                    $outcome['pay_time'] = time();

                    $res = $model->updateBgOrder(array('bg_order_id'=>$bg_order_info['bg_order_id']),$outcome);
                    $bargain_mode->true_member($bg_order_info['bargain_id']);
                    $bargain_mode->reduce_store($bg_order_info['bargain_id']);
                    $redis->delete($deploy['prefix'].'payorder-'.$bg_order_info['bg_order_id']);//释放redis中订单支付锁
                    $res && exit('success');
                }



            }
    }else{
            $log_->log_result($log_name, "【签名验证失败】:\n".$sign."\n".$data['data']['sign']."\n");
        }


    }




}


